<?php

App::uses('AppModel', 'Model');

/**
 * Client Model
 *
 */
class Client extends AppModel {

    public $table = 'orders';
//////////////////////////////////////////////////
    public $actsAs = array('CakePtbr.AjusteData', 'CakePtbr.AjusteFloat', 'Search.Searchable');
    public $filterArgs = array(
        'id' => array('type' => 'value'),
        'first_name' => array('type' => 'like'),
        'last_name' => array('type' => 'like'),
        'email' => array('type' => 'like'),
        'documento' => array('type' => 'like'),
        'phone' => array('type' => 'like'),
        'billing_address' => array('type' => 'like'),
        'billing_address2' => array('type' => 'like'),
        'billing_city' => array('type' => 'like'),
        'billing_zip' => array('type' => 'like'),
        'billing_state' => array('type' => 'like'),
        'billing_country' => array('type' => 'like'),
        'created' => array('type' => 'like'),
        'modified' => array('type' => 'like'),
    );
    public $validate = array(
        'first_name' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Primeiro nome invalido',
                'allowEmpty' => false,
                'required' => true,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'last_name' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Sobrenome invalido',
                'allowEmpty' => false,
                'required' => true,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'email' => array(
            'email' => array(
                'rule' => array('email'),
                'message' => 'Email invalido',
                'allowEmpty' => false,
                'required' => true,
            ),
            'UniqueEmail' => array(
                'rule' => array('checkUniqueEmail'),
                'required' => TRUE,
                'allowEmpty' => false,
                'message' => 'E-mail informado já existe.'
            ),
        ),
        'documento' => array(
            'documento' => array(
                'rule' => array('cnpjOuCpf','documento'),
                'required' => TRUE,
                'allowEmpty' => false,
                'message' => 'Documento invalido.'
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'UniqueDocumento' => array(
                'rule' => array('checkUniqueDocumento'),
                'required' => TRUE,
                'allowEmpty' => false,
                'message' => 'Documento informado já existe.'
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'phone' => array(
            'notempty' => array(
                'rule' => array('phone'),
                'message' => 'Phone is invalid',
                'allowEmpty' => false,
                'required' => true,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'billing_address' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Billing Address is invalid',
                'allowEmpty' => false,
                'required' => true,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'billing_city' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Billing City is invalid',
                'allowEmpty' => false,
                'required' => true,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'billing_state' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Billing State is invalid',
                'allowEmpty' => false,
                'required' => true,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    /**
     * This function is a simple function to check for a valid universal phone number - a useful extension for Cake Validation
     *
     * @param string|array $check Value to check
     * @return boolean Success
     */
    public function phone($check) {
        if (is_array($check)) {
            $value = array_shift($check);
        } else {
            $value = $check;
        }
        if (strlen($value) == 0) {
            return true;
        }
        return preg_match('/^[0-9-+()# ]{6,12}+$/', $value);
    }

    public function checkUniqueDocumento($data) {
        $isUnique = $this->find(
                'first', array(
            'fields' => array(
                'Client.id',
                'Client.documento',
            ),
            'conditions' => array(
                'Client.documento' => $data['documento']
            )
                )
        );

        if (!empty($isUnique)) {
            if (isset($this->data['Client']['id'])) {
                if ($this->data['Client']['id'] != '' AND $this->data['Client']['id'] != $isUnique['Client']['id']) {
                    return FALSE;
                }
            } else {
                return FALSE;
            }
        }
        return TRUE;
    }

    public function checkUniqueEmail($data) {
        $isUnique = $this->find(
                'first', array(
            'fields' => array(
                'Client.id',
                'Client.email',
            ),
            'conditions' => array(
                'Client.email' => $data['email']
            )
                )
        );

        if (!empty($isUnique)) {
            if (isset($this->data['Client']['id'])) {
                if ($this->data['Client']['id'] != '' AND $this->data['Client']['id'] != $isUnique['Client']['id']) {
                    return FALSE;
                }
            } else {
                return FALSE;
            }
        }
        return TRUE;
    }

}
